package com.wd.config;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.wd.util.PathUtil;
import com.wd.util.SplitUtil;

/**
 * @author long
 * @date 2013-5-4 下午4:11:50
 * @qq 108232706
 * @description Config
 */
public class Config {
	/**
	 * 系统主配置，key=config
	 */
	private static final Properties properties = new Properties();
	private static final Map<String, Properties> propertiesMap = new HashMap<String, Properties>();
	private static final Logger logger = LoggerFactory.getLogger(Config.class);
	static {
		propertiesMap.put("config", properties);
	}

	public static boolean initConfig(String path) {
		logger.info("ready to load properties :" + path);
		if ((path == null) || (path.equals(""))) {
			return false;
		}
		String key = PathUtil.getFileNameWithoutExtend(path);
		if (!loadProperties(path, properties)) {
			return false;
		}
		propertiesMap.put(key, properties);
		return true;
	}

	public static boolean addConfig(String path) {
		if ((path == null) || (path.equals(""))) {
			return false;
		}
		String key = PathUtil.getFileNameWithoutExtend(path);
		Properties _properties = new Properties();
		if (!loadProperties(path, _properties))
			return false;
		propertiesMap.put(key, _properties);
		return true;
	}

	private static boolean loadProperties(String path, Properties _properties) {
		try {
			path = PathUtil.getResourcePath(path);
			InputStream inputStream = new BufferedInputStream(new FileInputStream(path));
			_properties.load(inputStream);
			return true;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			logger.error(path);
			return false;
		} catch (IOException e) {
			e.printStackTrace();
			logger.error(path);
		}
		return false;
	}

	public static String getValue(String key) {
		if (properties == null) {
			return null;
		}
		return properties.getProperty(key);
	}

	public static int getInt(String key) {
		if (properties == null) {
			return 0;
		}
		String value = properties.getProperty(key);
		if (SplitUtil.isNumeric(value)) {
			return Integer.parseInt(value);
		}
		return 0;
	}

	/* 

	private static boolean loadProperties(String path) {
		
	}
	

	
	public static String parseGlobalConfig(String configPath) {
	    File configFile = new File(configPath);
	    if (!configFile.exists()) {
	        Log.error("Can not found config file, server start failed.");
	        return null;
	    }

	    File gatewayConfig = new File(configFile.getParent(), "global_config.xml");
	    if (!gatewayConfig.exists()) {
	        Log.error("Can not found gateway config file, server start failed.");
	        return null;
	    }
	    return gatewayConfig.getAbsolutePath();
	}*/
}
